package com.joolun.mall.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joolun.common.exception.CustomException;
import com.joolun.mall.entity.SkuBasic;
import com.joolun.mall.entity.SpuBasic;
import com.joolun.mall.mapper.SkuBasicMapper;
import com.joolun.mall.mapper.SpuBasicMapper;
import com.joolun.mall.service.ISpuBasicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商品SPU-基本信息Service业务层处理
 * 
 * @author www.joolun.com
 * @date 2025-05-20
 */
@Service
public class SpuBasicServiceImpl extends ServiceImpl<SpuBasicMapper, SpuBasic> implements ISpuBasicService
{
    @Autowired
    private SpuBasicMapper spuBasicMapper;

    @Autowired
    private SkuBasicMapper skuBasicMapper;

    /**
     * 查询商品SPU-基本信息
     * 
     * @param spuId 商品SPU-基本信息ID
     * @return 商品SPU-基本信息
     */
    @Override
    public SpuBasic selectSpuBasicById(Long spuId)
    {
        return spuBasicMapper.selectSpuBasicById(spuId);
    }

    @Override
    public List<SpuBasic> selectList(SpuBasic spuBasic){
        if(CollectionUtil.isEmpty(spuBasic.getSpuCodes()) && CollectionUtil.isEmpty(spuBasic.getSkuCodes())){
            throw new CustomException("spu或sku编码不能为空");
        }
        List<SpuBasic> spuBasics = new ArrayList<>();

        //按spu查询
        if(CollectionUtil.isNotEmpty(spuBasic.getSpuCodes())){
            spuBasics = this.list(
                    Wrappers.query(spuBasic).lambda()
                            .eq(SpuBasic::getTenantId, 1L)
                            .eq(SpuBasic::getSpuStatus, 0)
                            .eq(SpuBasic::getIsDelete, 0)
                            .in(CollectionUtil.isNotEmpty(spuBasic.getSpuCodes()), SpuBasic::getSpuCode, spuBasic.getSpuCodes())
                            .in(CollectionUtil.isNotEmpty(spuBasic.getSkuCodes()), SpuBasic::getSkuCodes, spuBasic.getSkuCodes())
            );
            if(CollectionUtil.isNotEmpty(spuBasics)){
                List<Long> spuIds = spuBasics.stream().map(SpuBasic::getSpuId).collect(Collectors.toList());
                List<SkuBasic> skuBasics = skuBasicMapper.selectList(
                        Wrappers.query(new SkuBasic()).lambda()
                                .eq(SkuBasic::getTenantId, 1L)
                                .eq(SkuBasic::getSkuStatus, 0)
                                .eq(SkuBasic::getIsDelete, 0)
                                .in(SkuBasic::getSpuId, spuIds)
                );
                if(CollectionUtil.isNotEmpty(skuBasics)){
                    Map<Long, List<SkuBasic>> skuGroups = skuBasics.stream().collect(Collectors.groupingBy(SkuBasic::getSpuId));
                    spuBasics.forEach(e -> {
                        e.setSkus(skuGroups.get(e.getSpuId()));
                    });
                }
            }
        }

        //按sku查询
        if(CollectionUtil.isNotEmpty(spuBasic.getSkuCodes())){
            List<SkuBasic> skuBasics = skuBasicMapper.selectList(
                    Wrappers.query(new SkuBasic()).lambda()
                            .eq(SkuBasic::getTenantId, 1L)
                            .eq(SkuBasic::getSkuStatus, 0)
                            .eq(SkuBasic::getIsDelete, 0)
                            .in(SkuBasic::getSkuCode, spuBasic.getSkuCodes())
            );
            if(CollectionUtil.isNotEmpty(skuBasics)){
                List<Long> spuIds = skuBasics.stream().map(SkuBasic::getSpuId).distinct().collect(Collectors.toList());
                spuBasics = this.list(
                        Wrappers.query(spuBasic).lambda()
                                .eq(SpuBasic::getTenantId, 1L)
                                .eq(SpuBasic::getSpuStatus, 0)
                                .eq(SpuBasic::getIsDelete, 0)
                                .in(CollectionUtil.isNotEmpty(spuIds), SpuBasic::getSpuId, spuIds)
                );
                Map<Long, List<SkuBasic>> skuGroups = skuBasics.stream().collect(Collectors.groupingBy(SkuBasic::getSpuId));
                spuBasics.forEach(e -> {
                    e.setSkus(skuGroups.get(e.getSpuId()));
                });
            }
        }
        spuBasics.forEach(e -> {
            e.setSpuName(getSplitName(e.getSpuName()));
        });

        return spuBasics;
    }

    /**
     * 截取spu名称
     * @param spuName
     * @return
     */
    public String getSplitName(String spuName) {
        if (spuName != null) {
            String[] split = spuName.split("-");
            if (split.length > 1) {
                List<String> invNames = new ArrayList<>();
                long toSkip = 0;
                for (String s : split) {
                    if (toSkip == 0) {
                        toSkip++;
                        continue;
                    }
                    invNames.add(s);
                }
                return String.join("-", invNames);
            }
        }
        return spuName;
    }

    /**
     * 查询商品SPU-基本信息列表
     * 
     * @param spuBasic 商品SPU-基本信息
     * @return 商品SPU-基本信息
     */
    @Override
    public List<SpuBasic> selectSpuBasicList(SpuBasic spuBasic)
    {
        return spuBasicMapper.selectSpuBasicList(spuBasic);
    }

    /**
     * 新增商品SPU-基本信息
     * 
     * @param spuBasic 商品SPU-基本信息
     * @return 结果
     */
    @Override
    public int insertSpuBasic(SpuBasic spuBasic)
    {
        return spuBasicMapper.insertSpuBasic(spuBasic);
    }

    /**
     * 修改商品SPU-基本信息
     * 
     * @param spuBasic 商品SPU-基本信息
     * @return 结果
     */
    @Override
    public int updateSpuBasic(SpuBasic spuBasic)
    {
        return spuBasicMapper.updateSpuBasic(spuBasic);
    }

    /**
     * 批量删除商品SPU-基本信息
     * 
     * @param spuIds 需要删除的商品SPU-基本信息ID
     * @return 结果
     */
    @Override
    public int deleteSpuBasicByIds(Long[] spuIds)
    {
        return spuBasicMapper.deleteSpuBasicByIds(spuIds);
    }

    /**
     * 删除商品SPU-基本信息信息
     * 
     * @param spuId 商品SPU-基本信息ID
     * @return 结果
     */
    @Override
    public int deleteSpuBasicById(Long spuId)
    {
        return spuBasicMapper.deleteSpuBasicById(spuId);
    }
}
